package pub.cleangao.mybatis.expression;

import lombok.Data;
import lombok.Setter;
import pub.cleangao.mybatis.runtime.MybatisContext;

import java.util.List;

@Data
public class JoinExpression implements FromExpression {


    public static JoinExpression buildExpression(FromJoint fromJoint, String table, String eqColumn, List<String> columns, String prevTable, String prevEqColumn, int... index) {
        JoinExpression joinExpression = new JoinExpression();
        joinExpression.setFromJoint(fromJoint);
        joinExpression.setTable(table);
        joinExpression.setTableAs(MybatisContext.getTableAs(table));
        joinExpression.setEqColumn(eqColumn);
        joinExpression.setColumns(columns);
        joinExpression.setPrevTable(prevTable);
        joinExpression.setPrevTableAs(MybatisContext.getTableAs(prevTable));
        joinExpression.setPrevEqColumn(prevEqColumn);
        if (index != null && index.length > 0) {
            joinExpression.setIndex(index[0]);
        }
        return joinExpression;
    }

    private int index;
    private FromJoint fromJoint;
    private String table;
    private String tableAs;
    private List<String> columns;
    private String eqColumn;
    private String prevTable;
    private String prevTableAs;
    private String prevEqColumn;

    @Override
    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public int index() {
        return index;
    }

    @Override
    public FromJoint joint() {
        return fromJoint;
    }

    @Override
    public String table() {
        return table;
    }

    @Override
    public List<String> columns() {
        return columns;
    }


}
